<?

/**	
 *	social weather object
 *	
 * 	the class is documented in the file itself. if you find any bugs help us out and report them. 
 * 	if you report a bug, make sure you give us enough information (include your code).
 *
 *	@author			pieter van peteghem <pieter@onedotonly.be>
 *
 *	@copyright		oneDot only <we@onedotonly.be>
 * 	@copyright		Copyright (c) 2010, oneDot only. All rights reserved.
 * 	@license		BSD License
 */


class SocialWeather extends MOD {

	/**
	 *	constructor
	 *	
	 *	add module vars and reference (multiple modules on 1 page are possible)	
	 *	@param		array ($vars)
	 */

	function __construct( $vars ) {
		$this->_addvars( $vars );
		$this->_ref();
	}
	
	
	/**
	 * 	define output
	 *
	 *	this is the base for rendering the module
	 *	while rendering the form css is added, an IE check is executed, javascript is added
	 */
	
	function output() {
		
		if( $this->action )
			$result = $this->{ "filter_" . $this->action }();

		// add css & show variables
		if ( $result ) {
			$this->addclass( $this->action );
			(( $result[0] ) ? $result = '<ul>' . implode( "\n", $result ) . '</ul>' : $result = '');
		}

		return (( $result )? $this->parse( $result ): '');
		
	}
	
	
	/**
	 *	render weather in list
	 */
	
	function filter_weather() {
		global $DB, $Engine, $Object;
		
		// get long -and latitude
		$ll 	= explode(',', (( $Object->loc) ? $Object->loc->get( 'll' ) : $Object->get('ll')));
		
		$long	= substr( str_replace(',', '', str_replace('.','', $ll[0])), 0, 8 );
		$lat	= substr( str_replace(',', '', str_replace('.','', $ll[1])), 0, 8 );
		
		$ll		= str_pad($long, 8, '0') . ',' . str_pad($lat, 8, '0');
		
		// new gweather
		$weather	= new GWeather();
		$weather	= $weather->weather( $ll, true, true, 4, Engine_get('lang.master') );
		
		// replace images
		$image = array_pop( explode( '/', $weather['image'] ));
		$image = array_shift( explode( '.', $image ));
		$image = 'http://central.onedotonly.be/style/modules/social.weather/' . $image . '.png';
		
		$return[]	= (( $weather['image'] != 'http://www.google.com' )? $this->render_li( '<img src="' . $image . '" />', 'image' ) : '');
		$return[]	= (( $weather['temp'] )? $this->render_li( $weather['temp'] . $this->vars->temp, 'temp' ) : '');
		$return[]	= (( $weather['description'] )? $this->render_li( $weather['description'], 'description' ) : '');
		$return[]	= (( $weather['wind'] )? $this->render_li( $weather['wind'], 'wind' ) : '');
		$return[]	= (( $weather['humidity'] )? $this->render_li( $weather['humidity'], 'humidity' ) : '');
		
		return $return;
		
	}
	
	
	/**
	 * 	render list items
	 *	@return		string 
	 *	@param		string (content)
	 *	@param		string (class)
	 */
	
	function render_li( $content, $class ) {
	
		return '<li class="' . $class . '">' . $content . '</li>';
	}
	
}



/** 
 * WORLD WEATHER ONLINE API CLASS
 *
 * This can be used to communicate with social weather api (http://www.google.com/ig/api?weather=)
 * This is only the beginning of a social weather class that makes it able to easily communicate with api.. should be expanded in the future!
 *
 * @author		Pieter Van Peteghem <pieter@onedotonly.be>
 * @version		version 0.01 beta
 *
 * @copyright	oneDot only <we@onedotonly.be> Copyright (c) 2010, oneDot only. All rights reserved.
 * @license		BSD License
 */

class Weather {

	/**
	 * url for the request
	 */
	const URL 		= 'http://www.worldweatheronline.com/feed/weather.ashx?q=';	

	
	/**
	 * api key
	 * @var 	string
	 */	
	private $apikey	= '';
	
	
	/**
	 * format of the answer
	 * @var 	string
	 */	
	private $format	= '';
	
	
	/**
	 * format of degrees
	 * @var		string
	 */
	private $degree	= '';
	
	
	/**
	 * constructor
	 * define api, place, degrees, format and number of days
	 * @param	api		api key
	 * @param	string	degree format
	 * @param	string	format
	 */
	function __construct( $api, $degree, $format ) {
	
		$this->set_api( $api );
		$this->set_degree( $degree );
		$this->set_format( $format );
	}
	
	
	/**
	 * set api key for communication
	 * @param	string
	 */
	function set_api( $api ) {
		
		$this->apikey = $api;
	}
	
	
	/**
	 * set format
	 * @param	string
	 */
	function set_format( $format ) {
		
		$this->format = $format;
	}
	
	
	/**
	 * set number of days
	 * @param	string
	 */
	function set_degree( $degree ) {
		
		$this->degree = $degree;
	}
	
	
	/**
	 * fetch data in url
	 * @param	mixed	place
	 * @return	array
	 */
	function weather( $place, $days ) {
	
		$json	= json_decode( @file_get_contents( self::URL . $place . '&format=' . $this->format . '&number_of_days=' . $days . '&key=' . $this->apikey ))->{data};
		return $json;
	}
	
	
}


/** 
 * GOOGLE WEATHER API CLASS
 *
 * This can be used to communicate with social weather api (http://www.google.com/ig/api?weather=)
 * This is only the beginning of a social weather class that makes it able to easily communicate with google api.. should be expanded in the future!
 *
 * @author		Pieter Van Peteghem <pieter@onedotonly.be>
 * @version		version 0.01 beta
 *
 * @copyright	oneDot only <we@onedotonly.be> Copyright (c) 2010, oneDot only. All rights reserved.
 * @license		BSD License
 */
 

class GWeather {
	
	// constant variables
	var $URL = 'http://www.google.com/ig/api?weather=';	
	
	
	/**
	 * get weather
	 *
	 * @param		string (could be ll but also 'gent')
	 * @param		boolean	(is first parameter string or long -and latitude) (auto says string)
	 * @param		boolean (define you want to see todays weather or forecast) (auto says current conditions)
	 * @param 		int (number of days in forecast you want to see (max 4) (second boolean should be false)
	 * @param		string (define output language) (2 character string)
	 * @return		array
	 */
	 
	function weather( $place, $ll = false, $today = true, $days = '', $lang ) {
	

		// define url
		$url = $this->url( $ll ) . $place . '&hl=' . $lang;
		$weather = $this->readXml( $url, $today, $days );
		
		return $weather;
	}
	
	
	
	/** 
	 * define url
	 *
	 * @param		boolean	(is first parameter string or long -and latitude)
	 * @return		string
	 */
	 	
	function url( $ll = false ) {
	
		$url = $this->URL;
		if ( $ll ) $url = $this->URL . ',,,';
		
		return $url;
	}
	
	
	
	/**
	 * read xml
	 *
	 * this function is to big now, could be written much more smaller
	 * feel free to contribute
	 *
	 * @param		string (given url)
	 * @param		boolean
	 * @param		string / array (defines which days you want to see
	 */
	 
	function readXml( $url, $today = true, $days = '' ) {
	
		$xml = simplexml_load_file( $url );
		$xml = $xml->weather;
		
		if ( $today ) {
		
			$wind = (array) $xml->current_conditions->wind_condition['data'];
			$wind = $wind[0];
				
			$descr = (array) $xml->current_conditions->condition['data'];
			$descr = $descr[0];
				
			$temp_c = (array) $xml->current_conditions->temp_c['data'];
			$temp_c = $temp_c[0];
				
			$humidity = (array) $xml->current_conditions->humidity['data'];
			$humidity = $humidity[0];
				
			$return = array( 
				"temp" => $temp_c,
				"wind" => $wind,
				"description" => $descr,
				"humidity" => $humidity,
				"image" => "http://www.google.com" . $xml->current_conditions->icon['data']
			);

		} else {
			
			// get array of the days forecast
			$forecast = $xml->forecast_conditions;
			
			// loop for the days
			$i = 0;
			while ( $i < $days ) {
				
				$day = (array) $forecast[$i]->day_of_week['data'];
				$day = $day[0];
				
				$descr = (array) $forecast[$i]->condition['data'];
				$descr = $descr[0];
				
				$return[] = array(
					"day" => $day,
					"description" => $descr,
					"low" => $this->fahrenheitToCelsius( $forecast[$i]->low['data'] ),
					"high" => $this->fahrenheitToCelsius( $forecast[$i]->high['data'] ),
					"average" => $this->fahrenheitToCelsius( $this->average ( (int) $forecast[$i]->low['data'],  (int) $forecast[$i]->high['data'] )),
					"image" => "http://www.google.com" . $forecast[$i]->icon['data']
				);
				
				$i++;
			}
		
		}
		
		return $return;
		
	}
	
	
	
	/** 
	 * fahrenheit to celsius
	 *
	 * @param 		int (degrees)
	 * @return 		int (degrees)
	 */
	
	function fahrenheitToCelsius( $degrees ) {
		
		$result = ( (int) $degrees - 32 ) / 1.8;
		return round($result);
	
	}
	
	
	
	/** 
	 * average between to integers
	 *
	 * @param		int 
	 * @param		int
	 * @return		int
	 */
	
	function average( $one, $two ) {
	
		$average	= ((int) $one + (int) $two) / 2;
		return round($average); 
	}


}


/** 
 * WORLD WEATHER ONLINE API CLASS
 *
 * This can be used to communicate with social weather api (http://www.worldweatheronline.com/)
 * This is only the beginning of a social weather class that makes it able to easily communicate with api.. should be expanded in the future!
 *
 * @author		Pieter Van Peteghem <pieter@onedotonly.be>
 * @version		version 0.01 beta
 *
 * @copyright	oneDot only <we@onedotonly.be> Copyright (c) 2010, oneDot only. All rights reserved.
 * @license		BSD License
 */

class Weather_newbie {

	/**
	 * url for the request
	 */
	const URL 		= 'http://www.worldweatheronline.com/feed/weather.ashx?q=';	

	
	/**
	 * api key
	 * @var 	string
	 */	
	private $apikey	= '';
	
	
	/**
	 * format of the answer
	 * @var 	string
	 */	
	private $format	= '';
	
	
	/**
	 * format of degrees
	 * @var		string
	 */
	private $degree	= '';
	
	
	/**
	 * constructor
	 * define api, place, degrees, format and number of days
	 * @param	api		api key
	 * @param	string	degree format
	 * @param	string	format
	 */
	function __construct( $api, $degree, $format ) {
	
		$this->set_api( $api );
		$this->set_degree( $degree );
		$this->set_format( $format );
	}
	
	
	/**
	 * set api key for communication
	 * @param	string
	 */
	function set_api( $api ) {
		
		$this->apikey = $api;
	}
	
	
	/**
	 * set format
	 * @param	string
	 */
	function set_format( $format ) {
		
		$this->format = $format;
	}
	
	
	/**
	 * set number of days
	 * @param	string
	 */
	function set_degree( $degree ) {
		
		$this->degree = $degree;
	}
	
	
	/**
	 * fetch data in url
	 * @param	mixed	place
	 * @return	array
	 */
	function weather( $place, $days ) {
	
		$json	= json_decode( @file_get_contents( self::URL . $place . '&format=' . $this->format . '&num_of_days=' . $days . '&key=' . $this->apikey ))->{data};
		return $this->transformer( $json );
	}
	
	
	/**
	 * transform the given data
	 * @param	object
	 * @return	object
	 */
	function transformer( $json ) {
	
		// current weather
		$weather->current 	= $json->current_condition[ 0 ];
		
		// feature weather
		$weather->future	= $json->weather;
		
		// request params
		$weather->request	= $json->request[ 0 ];
		
		return $weather;
	}
	
}

